package com.dc.bobaoge.project.v2.to.v3.business.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.base.entity.message.common.po.MessageCount;
import com.dc.bobaoge.base.mapper.message.MessageCountMapper;
import com.dc.bobaoge.common.em.MessageTemplateEnum;
import com.dc.bobaoge.common.util.YearQuarterUtil;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreAdvertisementBrowserCount;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreAdvertisementBrowserCountMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreAdvertisementMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessageBrowserCountResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-09 10:40:32
 */
@Slf4j
@Component
public class AdvertisementBrowserCountResolveMethod implements ResolveMethod {

    @Resource
    private MessageCountMapper messageCountMapper;

    @Resource
    private OldCoreAdvertisementBrowserCountMapper oldCoreAdvertisementBrowserCountMapper;
    @Resource
    private OldCoreAdvertisementMapper oldCoreAdvertisementMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 执行数据处理
        var wrapper = new LambdaQueryWrapper<OldCoreAdvertisementBrowserCount>()
                .eq(OldCoreAdvertisementBrowserCount::getAdvId, trigger.getTargetId());
        // 查询数据id
        var list = oldCoreAdvertisementBrowserCountMapper.selectList(wrapper);
        if (list.isEmpty()) {
            return;
        }
        // 查询广告的信息
        var oldCoreAdvertisement = oldCoreAdvertisementMapper.selectById(trigger.getTargetId());
        if (oldCoreAdvertisement == null) {
            return;
        }
        var quarter = YearQuarterUtil.get(oldCoreAdvertisement.getAdvmsgDate());
        var count = list.stream().mapToInt(OldCoreAdvertisementBrowserCount::getAdvBrowserCont).sum();
        var updateWrapper = new LambdaUpdateWrapper<MessageCount>()
                .eq(MessageCount::getFkMessageId, trigger.getTargetId())
                .eq(MessageCount::getType, MessageTemplateEnum.ADVERTISEMENT.getType())
                .eq(MessageCount::getQuarter, quarter)
                .set(MessageCount::getOldBrowserCount, count);
        messageCountMapper.update(updateWrapper);
    }


    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "adv_browser_count";
    }

}
